package com.seocoo.worker.base.multiImage;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.seocoo.worker.R;
import com.seocoo.worker.base.ImageSelector;
import com.seocoo.worker.base.PermissionUtil;
import com.seocoo.worker.base.refresh.SpaceItemDecoration;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: ssp
 * Date: 2020/4/14 19:03
 * Description:  展示多个图片
 */
public class BaseListImageFragment extends MVVMFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {

    private RecyclerView recycler_view;
    private BaseListImageAdapter imageAdapter;
    private int rows;
    private int counts;
    private boolean delete = false;
    private TextView tv_title2;
    private TextView tv_title1;
    private LinearLayout ll_title;

    @Override
    public int getLayout() {
        return R.layout.base_list_image_fragment;
    }

    @Override
    public void initData(View view) {
        recycler_view = view.findViewById(R.id.recycler_view);
        ll_title = view.findViewById(R.id.ll_title);
        tv_title1 = view.findViewById(R.id.tv_title1);
        tv_title2 = view.findViewById(R.id.tv_title2);
    }

    /**
     * @param row   每行展示个数
     * @param count 总的图片数量
     */
    public void setImageCount(int row, int count, boolean delete) {
        this.rows = row;
        this.counts = count;
        recycler_view.setLayoutManager(new GridLayoutManager(getContext(), rows));
        recycler_view.addItemDecoration(new SpaceItemDecoration(17));
        imageAdapter = new BaseListImageAdapter(delete);
        imageAdapter.bindToRecyclerView(recycler_view);
        imageAdapter.setOnItemClickListener(this);
        imageAdapter.setOnItemChildClickListener(this);
    }

    public void setTv_title1(String title1) {
        if (tv_title1 != null) {
            tv_title1.setText(title1);
        }
    }

    public void setTv_title2(String title2) {
        if (tv_title2 != null) {
            tv_title2.setText(title2);
        }
    }

    public void setVisbilityLLTitle(boolean visbilityLLTitle) {
        if (visbilityLLTitle) {
            ll_title.setVisibility(View.VISIBLE);
        } else {
            ll_title.setVisibility(View.GONE);
        }

    }

    /**
     * 直接展示数据
     *
     * @param list
     */
    public void setNewData(@NonNull List<RspImage> list) {
        imageAdapter.setNewData(list);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List<RspImage> datas = adapter.getData();
        if (datas.isEmpty()) {
            return;
        }
        RspImage rspImage = datas.get(position);
        if (TextUtils.equals("-1", rspImage.fullPath)) {
            //是新增图片的按钮
            PermissionUtil.checkPermissions(this, "请允许访问相册与摄像头", 1011, new PermissionUtil.PermissionCallBack() {
                @Override
                public void onHasPermission() {
                    int size = datas.size();
                    if (size >= counts) {
                        ImageSelector.create().count(1).showCamera(true).start(BaseListImageFragment.this, 1010);
                    } else {
                        ImageSelector.create().count(counts + 1 - size).showCamera(true).start(BaseListImageFragment.this, 1010);
                    }
                }

                @Override
                public void onFailedPermission() {
                    showDialogToast("授权失败！");
                }
            }, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);

        } else if (!TextUtils.equals("-1", rspImage.fullPath) && !TextUtils.isEmpty(rspImage.fullPath)) {
            //查看大图
            ArrayList<RspImage> list = new ArrayList<>();
            for (RspImage bean : datas) {
                if (!TextUtils.equals(bean.getFullPath(), "-1")) {
                    list.add(bean);
                }
            }
            LookImageActivity.startUi(getContext(), list, position);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1010) {
                List<String> images = ImageSelector.onActivityResult(resultCode, data);
                if (images != null && images.size() > 0) {
                    for (String bean : images) {
                        getViewModel(UpLoadImageModel.class).upLoadFile(bean);
                    }
                }
            }
        }

    }

    @LiveDataMatch
    public void onUpLoadSuccess(RspImage data) {
        List<RspImage> list = imageAdapter.getData();
        int size = list.size();
        if (size >= counts) {
            //达到最大可选图片数量
            imageAdapter.setData(size - 1, data);
        } else {
            imageAdapter.addData(0, data);
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        //删除图片
        List<RspImage> data = imageAdapter.getData();
        int size = data.size();
        if (size == counts) {
            //表示当前已经达到上限 删除需添加添加项
            RspImage image = data.get(counts - 1);
            if (TextUtils.equals(image.getFullPath(), "-1")) {
                //表示选择了5张照片 直接移除当前选择
                data.remove(position);
                imageAdapter.notifyDataSetChanged();
            } else {
                //选择了6张图片 移除当前选择并添加选择的图片
                data.remove(position);
                RspImage image1 = new RspImage();
                image1.setFullPath("-1");
                data.add(data.size(), image1);
                imageAdapter.notifyDataSetChanged();
            }
        } else {
            //选择图片小于等于4张直接移除当前位置
            data.remove(position);
            imageAdapter.notifyDataSetChanged();
        }

    }

    public List<RspImage> getImgList() {
        return imageAdapter.getData();
    }
}
